package com.ruizhong.integratedmediawithoutc.controller;


import com.alibaba.fastjson.JSON;
import com.ruizhong.integratedmediawithoutc.entity.TProgram;
import com.ruizhong.integratedmediawithoutc.server.WebSocketServer;
import com.ruizhong.integratedmediawithoutc.service.TProgramService;
import com.ruizhong.integratedmediawithoutc.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/program/api")
@CrossOrigin
public class ProgramController {

    private static HashSet<String> streamsSet = new HashSet<>();

    @Autowired
    private TProgramService tProgramService;

    @PostMapping("/addProgram")
    public Result addProgram(@RequestBody Map<String, Object> conditionMap) {
        System.out.println(conditionMap);
        ArrayList<HashMap<String, Object>> data = (ArrayList<HashMap<String, Object>>) conditionMap.get("data");
        HashMap<String, Object> programInfo = data.get(0);
        Integer programId = tProgramService.addProgram(programInfo);
        HashMap<String, Object> resMap = new HashMap<>();
        HashMap<String, Object> resMap1 = new HashMap<>();
        resMap.put("id",programId);
        resMap1.put("data",resMap);
        return new Result(ResultCode.success, "添加成功", resMap1);
    }

    @PostMapping("/addItemInProgram")
    public Result addItemInProgram(@RequestBody Map<String, Object> conditionMap) {
        System.out.println(conditionMap);
        ArrayList<HashMap<String, Object>> items = (ArrayList<HashMap<String, Object>>) conditionMap.get("data");
        tProgramService.addItemInProgram(items);
        return new Result(ResultCode.success, null, null);
    }

    @PostMapping("/deleteProgram")
    public Result deleteProgram(@RequestBody Map<String, Object> conditionMap) {
        tProgramService.deleteProgram(conditionMap);
        return new Result(ResultCode.success, "删除成功", null);
    }

    @DeleteMapping("/deleteAllProgram")
    public Result deleteAllPrograms(){
        tProgramService.deleteAllPrograms();
        return new Result(ResultCode.success,"删除成功",null);
    }

    private static Integer suffixIndex = 0;
    @PostMapping("/getProgramList")
    public Result getProgramList(@RequestBody Map<String, Object> conditionMap) {
        ++suffixIndex;
//        Map<String, Object> programResMap = TCPUtil.send(conditionMap);
//        ArrayList<Map<String, Object>> programs = (ArrayList<Map<String, Object>>) programResMap.get("data");
//        HashMap<String, Object> conditionMap1 = new HashMap<>();
//        conditionMap1.put("command", CMD.QueryR.ordinal());
//        conditionMap1.put("user_name",conditionMap.get("user_name"));
//        conditionMap1.put("data",null);
//        Map<String, Object> resourceResMap = TCPUtil.send(conditionMap1);
//        ArrayList<Map<String, Object>> resourceList = (ArrayList<Map<String, Object>>) resourceResMap.get("data");
//        Map<Object, Integer> nameMapId = Collections.EMPTY_MAP;
//        if (!CollectionUtils.isEmpty(resourceList)){
//            nameMapId = resourceList.stream().collect(Collectors.toMap(t -> t.get("file"), t -> {
//                return (int) t.get("id");
//            }));
//        }
//
//        if (!CollectionUtils.isEmpty(programs)) {
//            for (Map<String, Object> program : programs) {
//                program.put("suffix","active_live"+suffixIndex);
//                program.put("showSonPrograms", false);
//                ArrayList<Map<String, Object>> programItems = (ArrayList<Map<String, Object>>) program.get("programs");
//                Integer totalMS = 0;
//                if (!CollectionUtils.isEmpty(programItems)){
//                    for (Map<String, Object> programItem : programItems) {
//                        totalMS += (Integer) programItem.get("duration");
//                        programItem.put("id",nameMapId.get(programItem.get("media").toString()));
//                    }
//                }
//                Integer minute = totalMS / (1000 * 60);
//                Integer mSecond = totalMS % (1000 * 60);
//                program.put("pgDuration", minute + "分" + (mSecond / 1000) + "秒");
//                program.put("programType", "点播");
//                program.put("days", "\\");
//                program.put("time", "\\");
//            }
//        }
        List<Map<String, Object>> programList = tProgramService.getProgramList();

        HashMap<String, Object> resMap = new HashMap<>();
        resMap.put("data",programList);

        return new Result(ResultCode.success, null, resMap);
    }

    @PostMapping("/onDemand")
    public Result onDemand(@RequestBody Map<String,Object> conditionMap) throws IOException {
        Map<String, Object> resMap = TCPUtil.send(conditionMap);
        return new Result(ResultCode.success,null,resMap);
    }

    @PostMapping("/onDemandWithTrans")
    public Result onDemandWithTrans(@RequestBody Map<String,Object> conditionMap) throws IOException {
        String flvUrl = tProgramService.onDemandWithTrans(conditionMap);
        HashMap<String, Object> resMap = new HashMap<>();
        HashMap<String, String> urlMap = new HashMap<>();
        urlMap.put("flvUrl",flvUrl);
        resMap.put("data",urlMap);
        return new Result(ResultCode.success,null,resMap);
    }

    @PostMapping("/stopProgram")
    public Result stopProgram(@RequestBody Map<String,Object> conditionMap){
        Map<String, Object> resMap = TCPUtil.send(conditionMap);
        ArrayList<Map<String, Object>> data = (ArrayList<Map<String, Object>>) conditionMap.get("data");
        String pl_id = data.get(0).get("pl_id").toString();
        streamsSet.remove(pl_id);
        return new Result(ResultCode.success,null,resMap);
    }
}
